package com.wustl.android.hood;

import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Point;

import android.content.Context;
import android.database.Cursor;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewInflate;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ItemMainMapBar extends LinearLayout {
	private DbLocations locations;
	
	protected void onFinishInflate() {
		Button zoomIn = (Button)findViewById(R.id.map_zoomin);
		zoomIn.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) { zoom(true); }
		});
		
		Button zoomOut = (Button)findViewById(R.id.map_zoomout);
		zoomOut.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) { zoom(false); }
		});
		
		Button typeMap = (Button)findViewById(R.id.maptype_map);
		typeMap.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) { mapType(MAP_TYPE.MT_MAP); }
		});
		
		Button typeSatellite = (Button)findViewById(R.id.maptype_satellite);
		typeSatellite.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) { mapType(MAP_TYPE.MT_SATELLITE); }
		});
		
		Button typeTerrain = (Button)findViewById(R.id.maptype_traffic);
		typeTerrain.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) { toggleTraffic(); }
		});
	}
	
	private void addLoc(Point p, int zoom, int map_type, CharSequence name) {
		locations.createLocation(name.toString(), (double)p.getLatitudeE6()/1E6, (double)p.getLongitudeE6()/1E6, 0, zoom, map_type);
	}
	
	public ItemMainMapBar(Context context, AttributeSet attrs, Map inflateParams) {
		super(context, attrs, inflateParams);
		ViewInflate vi = (ViewInflate)context.getSystemService(Context.INFLATE_SERVICE);
		vi.inflate(R.layout.item_main_map_bar, this, true, inflateParams);
		locations = new DbLocations(context);
		locations.open();
		
//		LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//		List<LocationProvider> providers = lm.getProviders();
//		
//		// @TODO: List up provider test
//		if( providers != null ) {
//			StringBuffer sb = new StringBuffer();
//
//			ListIterator<LocationProvider> it = providers.listIterator();
//			
//			while( it.hasNext() ) {
//				LocationProvider pr = it.next();
//				sb.append( pr.getName() );
//			}
//			
//			final TextView edit = (TextView) findViewById(R.id.new_label);
//			edit.setText(sb);
//			ImageView add = (ImageView)findViewById(R.id.new_button);
//			add.setOnClickListener(new OnClickListener() {
//				public void onClick(View arg0) {
//					MapView mv = ((MapView)findViewById(R.id.map));
//					int map_type = DbLocations.convertMapType( MAP_TYPE.MT_MAP );
//					if( mv.isSatellite() )
//						map_type = DbLocations.convertMapType( MAP_TYPE.MT_SATELLITE );
//					addLoc(mv.getMapCenter(), mv.getZoomLevel(), map_type, edit.getText());
//				}
//			});
//		}

		// Location of Washington University
		double lat = 38.647059;
		double lon = -90.305707;

		moveTo(lat, lon, 14);
	}
	
	public void zoom(boolean zoomIn) {
		MapView map = (MapView) findViewById(R.id.map);
		MapController mc = map.getController();

		if( zoomIn )
			mc.zoomTo( map.getZoomLevel() + 1 );
		else
			mc.zoomTo( map.getZoomLevel() - 1 );
	}
	
	public void mapType( MAP_TYPE type) {
		MapView map = (MapView) findViewById(R.id.map);
		if( type == MAP_TYPE.MT_MAP ){
			if( map.isSatellite() == true )
				map.toggleSatellite();
		}
		else if( type == MAP_TYPE.MT_SATELLITE ){
			if( map.isSatellite() == false )
				map.toggleSatellite();
		}
	}
	
	public void toggleTraffic() {
		MapView map = (MapView) findViewById(R.id.map);
		map.toggleTraffic();
	}
	
	public void moveTo(double lat, double lon, int zoom ) {
		MapView map = (MapView) findViewById(R.id.map);
		MapController mc = map.getController();
		
		com.google.android.maps.Point p = new com.google.android.maps.Point((int) (lat * 1E6), (int) (lon * 1E6));
		mc.animateTo(p);
		mc.zoomTo(zoom);
	}
	
	public void setMapType( MAP_TYPE mt ) {
		MapView map = (MapView) findViewById(R.id.map);
		if( (mt == MAP_TYPE.MT_MAP && map.isSatellite() )
			|| (mt == MAP_TYPE.MT_SATELLITE && !map.isSatellite() ) )
			map.toggleSatellite();
	}
	
	public void setCurrentLocation(Cursor row) {
		final double latitude = row.getDouble(row.getColumnIndex( DbLocations.KEY_LATITUDE ));
		final double longitude = row.getDouble( row.getColumnIndex( DbLocations.KEY_LONGITUDE ) );
		final int zoom = row.getInt( row.getColumnIndex( DbLocations.KEY_ZOOM ));
		final int map_type = row.getInt( row.getColumnIndex( DbLocations.KEY_MAPTYPE ));
		moveTo(latitude, longitude, zoom);
		setMapType( DbLocations.convertMapType( map_type ));
	}
}
